<?php
namespace app\service;
use think\facade\Db;
class roominfoService{
    //查询小区数据
    public function communityList($where){
        return Db::name('community')->where($where)->select();
    }
    //判断小区是否重复
    public function roominfoName($community_id,$group,$building,$unit,$room){
        return Db::name('room')
                ->where('community_id',$community_id)
                ->where('group',$group)
                ->where('building',$building)
                ->where('unit',$unit)
                ->where('room',$room)
                ->find();
    }
    //添加小区信息
    public function roominfoAdd($data){
        return Db::name('room')->insert($data);
    }
    //查询指定ID房屋信息
    public function roominfoIdList($id){
        return Db::name('room')->where('id',$id)->find();
    }
    //修改指定ID房屋信息
    public function roominfoEdit($data){
        return Db::name('room')->update($data);
    }
    //查询指定id是否存在
    public function checkRoomInfo($data){
        if (is_array($data)) {
            return Db::name('room')->where('id', 'in', $data)->select();
        }else{
            return Db::name('room')->where('id', $data)->find();
        }
    }
    //删除指定的数据
    public function deleteRoomInfo($data) {     
            return Db::name('room')->where('id','in',$data)->delete();
    }
//------------------------------批量添加房屋信息-----------------------------------------
    //通过小区ID查询对应的小区名称
    public function communityIdName($community_id){
        return Db::name('community')->where('id', $community_id)->find();
    }
    //通过小区名称查询对应的小区id
    public function communityNameId($name){
        return Db::name('community')->where('name', $name)->find();
    }
    //通过Excel插入数据
    public function roomExcelAdd($data){
        return Db::name('room')->insertGetId($data);
    }
    //查询房间号是否重复
    public function roomComAddressGroup($community_id,$group,$address){
        return Db::name('room')->where('community_id',$community_id)->where('group',$group)->where('address',$address)->find();
    }
    public function roomComAddress($community_id,$address){
        return Db::name('room')->where('community_id',$community_id)->where('address',$address)->find();
    }
//------------------------------多条件查询房屋信息-----------------------------------------
    //根据小区名称查找楼栋信息
    public function groupList($community_id){
        return Db::name('room')->where('community_id',$community_id)->column('group');
    }
    //根据小区名称查找楼栋信息
    public function buildingList($community_id,$group){
        if(empty($group)){
            return Db::name('room')->where('community_id',$community_id)->order('id asc')->column('building');
        }
        return Db::name('room')->where('community_id',$community_id)->where('group',$group)->order('id asc')->column('building');
    }
    //根据楼栋查询单元
    public function unitList($community_id,$group,$building){
        if(empty($group)){
            return Db::name('room')->where('community_id',$community_id)->where('building',$building)->order('id asc')->column('unit');
        }
        return Db::name('room')->where('community_id',$community_id)->where('group',$group)->where('building',$building)->order('id asc')->column('unit');
    }
    //根据单元查询房屋
    public function roomList($community_id,$group,$building,$unit){
        if(empty($group)){
            return Db::name('room')->where('community_id',$community_id)->where('building',$building)->where('unit',$unit)->column('room');
        }        
        return Db::name('room')->where('community_id',$community_id)->where('group',$group)->where('building',$building)->where('unit',$unit)->column('room');
    }
    //多条件查询
    public function roomWhereList($community_id,$page, $where){
        return Db::name('room')->where('community_id', 'in', $community_id)->where($where)->order('id asc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    //查询条数
    public function roomWhereNumber($community_id,$where){
        return Db::name('room')->where('community_id', 'in', $community_id)->where($where)->count();
    }
	//今年欠款
    public function yearFee($address,$community_id){
        $year = date('Y',time());
        $time = $year.'-01-01 00:00:00';
        $yearFee = Db::name('bill')->where('community_id',$community_id)->where('room_address', $address)->where('time',">",$time)->where('status',"WAIT_PAYMENT")->sum('bill_amount');
        Db::name('room')->where('community_id',$community_id)->where('address',$address)->update(['year_fee'=>$yearFee]);
        return $yearFee;
		}
	 //历年欠款
    public function beforeFee($address,$community_id){
		 $year = date('Y',time());
        $time = $year.'-01-01 00:00:00';
      $beforeFee = Db::name('bill')->where('community_id',$community_id)->where('room_address', $address)->where('time',"<",$time)->where('status',"WAIT_PAYMENT")->sum('bill_amount');
        Db::name('roominfo')->where('community_id',$community_id)->where('address',$address)->update(['before_fee'=>$beforeFee]);

        return $beforeFee;
   
    }
}